<template>
  <div>
    <NormalForm pageTitle="企业朋友圈" :operateForm="queryForm" :formList="formList" @handleQuery="handleQuery" @handleReset="handleReset"></NormalForm>
    <div class="g-card">
      <div class="right-btn">
        <el-button icon="el-icon-plus" type="primary" size="mini" style="height: 32px; margin-left: 80px" v-hasPermi="['admin:moments:addOrUpdate']" @click="addDispatch">新建动态
        </el-button>
      </div>
      <div style="padding: 0 20px 20px 20px">
        <!-- 表格 -->
        <el-table v-hasPermi="['admin:moments:list']" :loading="loading" :data="tableList" style="width: 100%" height="415">
          <el-table-column type="index" width="55" align="center" label="序号" />
          <el-table-column label="动态内容" align="center" prop="content" show-overflow-tooltip></el-table-column>
          <el-table-column label="创建者" prop="createBy" align="center" :width="160"></el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" :width="160" />
          <el-table-column label="同步动态员工" align="center" prop="noAddUserName" show-overflow-tooltip></el-table-column>
          <el-table-column label="发送时间" align="center" prop="pushTime" width="160px"></el-table-column>
          <el-table-column label="状态" align="center" prop="isLwPush">
            <template slot-scope="{ row }">
              {{ row.isLwPush && row.isLwPush === "0" ? "待推送" : "已推送" }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template slot-scope="{ row }">
              <div class="option-span">
                <span v-if="row.isLwPush && row.isLwPush === '0'" v-hasPermi="['admin:moments:add']" @click="send(row)">发送</span>
                <span v-if="row.isLwPush && row.isLwPush === '0'" v-hasPermi="['admin:moments:update']" @click="edit(row)">编辑</span>
                <span v-if="row.isLwPush && row.isLwPush === '1'" v-hasPermi="['admin:moments:info']" @click="check(row)">详情</span>
                <span v-if="row.isLwPush && row.isLwPush === '0'" v-hasPermi="['admin:moments:delete']" @click="cancel(row)">删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
      </div>
    </div>
  </div>
</template>
<script>
import { queryTaskList, sendOrUpdateTask, deleteOrUpdateTask } from '../../../api/communityOperating/enterpriseCircle'

export default {
  name: 'enterpriseCircle',
  data() {
    return {
      queryForm: {
        deptId: null,
        createList: [],
        pageNum: 1,
        pageSize: 20,
      },
      formList: [
        {
          label: '管理机构',
          value: null,
          prop: 'deptId',
          type: 'MANAGE_COM',
          emunName: '',
        },
        {
          label: '创建人',
          value: '',
          prop: 'createList',
          type: 'STAFF',
          emunName: '',
        },
      ],
      total: 0,
      loading: false,
      tableList: [],
      dataDto: '',
    }
  },
  mounted() {
    this.handleQuery()
  },
  methods: {
    // 重置
    handleReset() {
      this.queryForm = {
        deptId: null,
        createList: '',
        pageNum: 1,
        pageSize: 20,
      };
      this.queryTaskListFn();
    },
    // 远程搜索任务名称 + 搜索
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.queryTaskListFn();
    },
    queryTaskListFn() {
      this.loading = true;
      let obj = {
        deptId: this.queryForm.deptId,
        createList: this.queryForm.createList || [],
        pageNum: this.queryForm.pageNum,
        pageSize: this.queryForm.pageSize,
      }
      queryTaskList(obj).then((res) => {
        if (res.code === 200) {
          this.tableList = res.rows || [];
          this.total = res.total || 0;
        }
        this.loading = false;
      })
    },
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.queryTaskListFn()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.queryTaskListFn()
    },
    // 发送
    async send(row) {
      await this.getSendtime({ moId: row.moId, flag: '1' })
      this.$confirm(
        '预计：' + this.dataDto + '发送动态，是否立即发送?',
        '发送确认',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        this.getSendtime({ moId: row.moId, flag: '2' })
      }).catch(() => {})
    },
    // 获取发送时间
    async getSendtime(obj) {
      this.dataDto = ''
      var datas = await sendOrUpdateTask(obj)
      if (datas.code === 200) {
        if (obj.flag !== '1') {
          this.handleQuery()
          this.$message.success(datas.data.dataDto)
        } else {
          this.dataDto = datas.data.dataDto
        }
      } else {
        this.$message.error(datas.msg)
      }
    },
    // 新建群发
    addDispatch() {
      this.$router.push({
        name: 'AddTrends',
        query: {
          operateFlag: 'ADD',
        },
      })
    },
    // 编辑
    edit(row) {
      this.$router.push({
        name: 'AddTrends',
        query: {
          rowContent: row,
          operateFlag: 'EDIT',
        },
      })
    },
    // 详情
    check(row) {
      this.$router.push({
        name: 'Detail',
        query: {
          rowContent: row,
          operateFlag: 'DETAIL',
        },
      })
    },
    // 删除
    cancel(row) {
      this.$confirm('此操作将确认删除动态信息, 是否继续?', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteOrUpdateTask({ moId: row.moId }).then((res) => {
          if (res.code === 200) {
            this.handleQuery()
            this.$message.success(res.data)
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {})
    },
  },
}
</script>

<style lang="scss" scoped>
@import '../../../styles/base.scss';
::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border-bottom: none !important;
}
::v-deep .el-table .el-table__header-wrapper th {
  background-color: #f8f8f9 !important;
}
</style>
